import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/home/home.vue";
// @  是一个绝对路径 指向的是src
import Login from "@/views/login/login.vue";
import rights from "@/views/rights/rights.vue";
import users from "@/views/users/users.vue";
import welcome from "@/views/welcome/welcome.vue";
import roles from "@/views/roles/roles.vue";
import  Reports from "@/views/reports/reports.vue"
import  goods from "@/views/goods/goods.vue"
import  params from "@/views/params/params.vue"
import  categories from "@/views/categories/categories.vue"
import  orders from "@/views/orders/orders.vue"
Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/home" },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    children:[

      {
        path: "/rights",
        name: "rights",
        component: rights,
      },
      {
        path: "/users",
        name: "users",
        component: users,
      },
      {
        path: "/welcome",
        name: "welcome",
        component: welcome,
      },
      {
        path: "/roles",
        name: "roles",
        component: roles,
      },
      {
        path: "/reports",
        name: "reports",
        component: Reports,
      },
      {
        path: "/goods",
        name: "goods",
        component: goods,
      },
      {
        path: "/params",
        name: "params",
        component: params,
      },
      {
        path: "/categories",
        name: "categories",
        component: categories,
      },
      {
        path: "/categories",
        name: "categories",
        component: categories,
      },
      {
        path: "/orders",
        name: "orders",
        component: orders,
      }
    ]
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  let token = window.localStorage.getItem("myToken");
  // 表示已经登录了
  if (token) {
    next();
  } else {
    //没有token  表示没有登录
    if (to.path == "/login") {
      next();
    } else {
      next({ path: "/login" });
    }
  }
});

export default router;
